package com.kuanter.kuanterauto.activity;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.kuanter.kuanterauto.R;
import com.kuanter.kuanterauto.baseui.BaseActivity_NoBar;
import com.kuanter.kuanterauto.constants.HttpConfigAPI;
import com.kuanter.kuanterauto.constants.MainConstants;
import com.kuanter.kuanterauto.entity.CarInsurance;
import com.kuanter.kuanterauto.net.HttpTask;
import com.kuanter.kuanterauto.utils.DataLoader;
import com.kuanter.kuanterauto.utils.JsonParser;
import com.kuanter.kuanterauto.utils.SharePrefenceUtil;
import com.umeng.analytics.MobclickAgent;

public class FinancialActivity extends BaseActivity_NoBar {
	RelativeLayout insurance_top;
	LayoutInflater factory;
	TextView add_insurance;
	LinearLayout liner_cx;
	
	Handler mHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			finishProgress();
			if(msg.what == HttpTask.RESULT_OK) {
				String json = (String) msg.obj;
				Log.i("bbb", json);
				List<CarInsurance> list = JsonParser.getFinancial(json);
				for(int i = 0; i < list.size(); i++) {
					final CarInsurance car = list.get(i);
					
					View cx = factory.inflate(R.layout.financial_item, null);
					ImageView lcimg = (ImageView) cx.findViewById(R.id.cximg);
					TextView lcname = (TextView) cx.findViewById(R.id.cxname);
					TextView lcad = (TextView) cx.findViewById(R.id.cxad);
					TextView dh = (TextView) cx.findViewById(R.id.bxdh);
					dh.setVisibility(View.GONE);
					TextView lcdh = (TextView) cx.findViewById(R.id.xbdh);
					MerchantTurnActivity1.DownLoadImage dli = new MerchantTurnActivity1.DownLoadImage(150, 150, lcimg);
					dli.execute(car.getInsuranceImgUrl());
					
					lcname.setText(car.getInsuranceName());
					lcad.setText(car.getInsuranceAd());
					dh.setText(car.getInsuranceTel());
//					lcdh.setText(car.getPhoneName1() + ": " + car.getInsuranceTel());
					lcdh.setText("理财电话:" + car.getLcdh());
					liner_cx.addView(cx);
					
					lcdh.setOnClickListener(new OnClickListener() {
						@Override
						public void onClick(View v) {
							if(!car.getLcdh().equals("暂无")) {
								new AlertDialog.Builder(FinancialActivity.this)
								.setMessage("是否拨打电话: " + car.getLcdh())
								.setPositiveButton("确认", new DialogInterface.OnClickListener() {
									@Override
									public void onClick(DialogInterface dialog, int which) {
										Intent intent_call_lc = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + car.getLcdh()));
										startActivity(intent_call_lc);
									}
								})
								.setNegativeButton("取消", null)
								.create().show();
								
							}
						}
					});
					
				}
				
				
			} else {
				Toast.makeText(FinancialActivity.this, "网络错误", 3000).show();
			}
			
			super.handleMessage(msg);
		}
		
	};
	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.financial);
		showProgress("正在加载数据...");
		DataLoader loader = new DataLoader(this);
		String url = MainConstants.HOST_test + HttpConfigAPI.findFinancial;
		List<NameValuePair> list = new ArrayList<NameValuePair>();
		NameValuePair params = new BasicNameValuePair("userId", SharePrefenceUtil.getUserID(FinancialActivity.this) + "");
		list.add(params);
		loader.DisplayData(url, list, mHandler, "", false);
		
		factory = getLayoutInflater();
		
		initView();
		
	}
	
	@Override
	protected void onPause() {
		MobclickAgent.onPause(this);
		super.onPause();
	}

	@Override
	protected void onResume() {
		MobclickAgent.onResume(this);
		super.onResume();
	}



	private void initView() {
		liner_cx = (LinearLayout) findViewById(R.id.liner_cx);
		
		insurance_top = (RelativeLayout) findViewById(R.id.insurance_top);
		TextView title = (TextView) insurance_top.findViewById(R.id.top_middle);
		title.setText("我的理财");
		
		LinearLayout home_chose_City = (LinearLayout) insurance_top.findViewById(R.id.home_chose_City);
		home_chose_City.setVisibility(View.GONE);
	}

	public void exit(View view) {
		finish();
	}

}
